package io.jsen.meta.diff.support;

import io.jsen.meta.diff.field.MetaField;
import io.jsen.meta.diff.obj.MetaObject;

import java.util.Collection;
import java.util.Map;

/**
 * @author jsen
 * @date 2021/04/30
 **/
public class MetaObjectSupport {

    public static Object newNativeObject(MetaObject<?, ?> object)
            throws IllegalAccessException, InstantiationException {
        Class<?> nativeObjectClass = object.getMetaRelation()
                .getMetaField().getTrueClass();
        return nativeObjectClass.newInstance();
    }


    public static void setValue(MetaField metaField, Object parentObj, Object o) {
        if (parentObj == null) {
            return;
        }
        if (parentObj instanceof Collection) {
            ((Collection) parentObj).add(o);
        } else if (parentObj instanceof Map) {
            ((Map) parentObj).put(metaField.getKey(), o);
        } else {
            metaField.getFieldDefine().setValue(parentObj, o);
        }
    }

    private MetaObjectSupport() {
    }
}
